package com.ay.wiki_plt.service.Impl;

import com.ay.wiki_plt.common.BusinessExceptionCode;
import com.ay.wiki_plt.common.PageBean;
import com.ay.wiki_plt.domain.Content;
import com.ay.wiki_plt.domain.Doc;
import com.ay.wiki_plt.domain.dto.DocSaveReq;
import com.ay.wiki_plt.exception.BusinessException;
import com.ay.wiki_plt.mapper.ContentMapper;
import com.ay.wiki_plt.mapper.DocMapper;
import com.ay.wiki_plt.service.DocSerivce;
import com.ay.wiki_plt.service.WsService;
import com.ay.wiki_plt.utils.CopyUtil;
import com.ay.wiki_plt.utils.RedisUtil;
import com.ay.wiki_plt.utils.RequestContext;
import com.ay.wiki_plt.utils.SnowFlake;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DocServiceImpl implements DocSerivce {
    @Resource
    private DocMapper docMapper;


    @Autowired
    private SnowFlake snowFlake;


    @Resource
    private ContentMapper contentMapper;

    @Resource
    public RedisUtil redisUtil;

    @Resource
    private WsService wsService;


    /*查询全部文档*/
    @Override
    public List<Doc> list(Long ebookId) {
        return docMapper.list(ebookId);
    }


    /*查询全部文档 分页查询*/
    @Override
    public PageBean list(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Doc> docList = docMapper.page(name);
        Page<Doc> pageInfo = (Page<Doc>) docList;
        return new PageBean(pageInfo.getTotal(), pageInfo.getResult());
    }


    /*保存和修改文档*/
    @Transactional
    @Override
    public void save(DocSaveReq req) {
        Doc doc = CopyUtil.copy(req, Doc.class);
        Content content = CopyUtil.copy(req, Content.class);
        Long docId = doc.getId();
        if (docId == null) {
            //保存
            doc.setId(snowFlake.nextId());

            docMapper.insertDoc(doc);
            //内容新增
            content.setId(doc.getId());
            doc.setViewCount(0);
            doc.setVoteCount(0);
            contentMapper.insert(content);
        } else {
            //修改
            docMapper.updateDoc(doc);
            //blob富文本
            int count = contentMapper.updateBlob(content);
            if (count == 0) {
                //如果数据为0执行插入
                contentMapper.insert(content);
            }
        }
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        docMapper.deleteBatchIds(ids);
    }


    /*文档内容查询*/
    @Override
    public String findContent(Long id) {
        //文档阅读数+1
        docMapper.updateDocView(id);
        return contentMapper.findContent(id);
    }

    /*查询全部文档*/
    @Override
    public List<Doc> getDocList() {
        return docMapper.getDocList();
    }

    /*点赞功能*/
    @Override
    public void vote(Long id) {
        //docMapper.vote(id);
        //远程ip+doc.id作为key,24小时内不能重复
        String ip = RequestContext.getRemoteAddr();
        if (redisUtil.validateRepeat("DOC_VOTE_" + id + "_" + ip, 3600 * 24)) {
            docMapper.vote(id);
        } else {
            throw new BusinessException(BusinessExceptionCode.VOTE_REPEAT);
        }
        //推送消息
        Doc docDb = docMapper.listOne(id);
        String logId = MDC.get("LOG_ID");
        wsService.sendInfo("【" + docDb.getName() + "】被点赞！", logId);

    }


    /*电子书信息定时更新*/
    @Override
    public void updateEbookInfo() {
        docMapper.updateEbookInfo();
    }
}
